package com.cliff.joinus.activity.fragment;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.cliff.joinus.JoinusMainBaseFragment;
import com.cliff.joinus.LocalMediaDataManager;
import com.cliff.joinus.R;
import com.cliff.joinus.activity.BrowseImageActivity;
import com.cliff.joinus.activity.BrowseMusicActivity;
import com.cliff.joinus.activity.BrowseVideoActivity;

public class LocalMediaFragment extends JoinusMainBaseFragment {
	
	protected ArrayAdapter<ArrayList<?>> mAdapter = null;

	//0:image; 1:music; 2:video
	@SuppressWarnings("rawtypes")
	protected final ArrayList list[] = new ArrayList[] {
			LocalMediaDataManager.getInstance().getImageList(),
			LocalMediaDataManager.getInstance().getMusicList(),
			LocalMediaDataManager.getInstance().getVideoList()
			};

	protected final int iconArray[] = new int[]{
			R.drawable.folder_image,
			R.drawable.folder_music,
			R.drawable.folder_video
			};
	
	protected final int nameArray[] = new int[]{
			R.string.tab_local_image,
			R.string.tab_local_music,
			R.string.tab_local_video
			};

	protected final int descArray[] = new int[]{
			R.string.tab_local_image_count,
			R.string.tab_local_music_count,
			R.string.tab_local_video_count
			};
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		 mAdapter = new ArrayAdapter<ArrayList<?>>(getActivity(),
				R.layout.item_line_name_desc, list) {
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {

				ArrayList<?> data = getItem(position);
				String name = getString(nameArray[position]);
				String descRes = getString(descArray[position]);
				int itemCount = data.size();
				String desc = String.format(descRes, itemCount);
				return getItemView(convertView, iconArray[position], name, desc);
			}
		};

		setListAdapter(mAdapter);
		return super.onCreateView(inflater, container, savedInstanceState);
	}

	@Override
	public void onDestroyView() {
		// TODO Auto-generated method stub
		mAdapter = null;
		super.onDestroyView();
	}


	@Override
	public void refresh() {
		// TODO Auto-generated method stub
		mAdapter.notifyDataSetChanged();
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		switch(position) {
		case 0://local image
			startActivity(new Intent(getActivity(), BrowseImageActivity.class));
			break;
		case 1://local music
			startActivity(new Intent(getActivity(), BrowseMusicActivity.class));
			break;
		case 2://local video
			startActivity(new Intent(getActivity(), BrowseVideoActivity.class));
			break;
		}
//		super.onListItemClick(l, v, position, id);
	}

}
